﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using QLDBHYT.DAO;
using QLDBHYT.VO;
using QLDBHYT.BUS;
using CrystalDecisions.CrystalReports.Engine;
using CrystalDecisions.Shared;
using CrystalDecisions.ReportSource;
using System.Data.OleDb;

namespace QLDBHYT
{
    public partial class frmKCBNoitru : Form
    {
        private QLDBHYT.VO.P2hVO p2hVO;
        private P2hBUS p2hBUS;
        SttDAO sttDAO;
        SttVO sttVO;
        PhieunhapBUS phieunhapBUS;
        private List<DoituongVO> lstDoituong;
        private List<P2duVO> lstP2duVO;
        private List<P2dVO> lstP2dVO;
        private string vanchuyen;
        private bool flagGhi = false;
        private string matinhtam = "56";
        internal QLDBHYT.VO.P2hVO P2hVO
        {
            get { return p2hVO; }
            set { p2hVO = value; }
        }
        public frmKCBNoitru()
        {
            InitializeComponent();
        }

        private void frmKCBNoitru_Load(object sender, EventArgs e)
        {
            p2hBUS = new P2hBUS();
            phieunhapBUS = new PhieunhapBUS();
            sttDAO = new SttDAO();
            GetKhoakham();
            GetThuoc();
            FormatFa();
            lstDoituong = p2hBUS.GetDoiTuong();
            GetMabv();
            ClearControl();
            docfiledbf();
        }
        private bool CheckDoiTuong(string madt, string loaitt)
        {
            var items = from item in lstDoituong
                        where item.Loaitt.Contains(loaitt) && item.Madt == madt
                        select item;

            List<DoituongVO> lstDoituong1 = items.ToList<DoituongVO>();
            if (lstDoituong1.Count >= 1)
            {
                vanchuyen = lstDoituong1[0].Vanchuyen;
                return true;
            }
            else
            {
                vanchuyen = "";
                return false;
            }

        }
        private void FormatFa()
        {
            fa.Cols[2].Editor = cboThuoc;
            fa.Cols[2].Editor.Width = 800;
        }
        private void txtMathe_Leave(object sender, EventArgs e)
        {
            P2hVO p2h;
            if (cboDT.SelectedIndex != 0) { return; }
            if (txtMathe.Text == "") { return; }
            if (txtMathe.Text.Length < 15 && txtMathe.Text.Length > 0)
            {
                MessageBox.Show("Nhập lại mã KCB");
                return;
            }
            if (!CheckDoiTuong(txtMathe.Text.Substring(0, 2), txtMathe.Text.Substring(2, 1)))
            {
                MessageBox.Show("Mã đối tượng không hợp lệ");
                return;
            }
            p2h = p2hBUS.GetMathe(txtMathe.Text);
            txtHoten.Text = p2h.Hoten;
            txtGioitinh.Text = p2h.Gioitinh.ToString();
            txtNgaysinh.Text = p2h.Ngaysinh.ToString();
            txtThangsinh.Text = p2h.Thangsinh.ToString();
            txtNamsinh.Text = p2h.Namsinh.ToString();
            txtDiachi.Text = p2h.Diachi;
            txtGTTU.Value = p2h.Gttu;
            txtGTDen.Value = p2h.Gtden;
            txtMatinh.Text = p2h.Tinhkcb;
            if (p2h.Makcb != null)
            {
                cboMabv.SelectedValue = p2h.Makcb;
                txtTenBV.Text = p2h.Benhvien;
            }
            else
            {
                txtMatinh.Text = "56";
                cboMabv.SelectedValue = "161";
                txtTenBV.Text = "Phßng kh¸m ®a khoa khu vùc 2";
            }
        }
        private void btnMoi_Click(object sender, EventArgs e)
        {
            ClearControl();
        }
        private void ClearControl()
        {
            flagGhi = false;
            lstP2duVO = new List<P2duVO>(14);
            lstP2dVO = new List<P2dVO>();
            txtMathe.Text = "";
            txtMathe.Focus();
            cboDT.SelectedIndex = 0;
            cboDieutri.SelectedIndex = 0;
            cboMakhoa.SelectedIndex = 0;
            txtNgayvao.Value = DateTime.Now;
            txtNgayra.Value = DateTime.Now;
            txtNgayTT.Value = DateTime.Now;
            txtGTTU.Value = DateTime.Now;
            txtGTDen.Value = DateTime.Now;
            txtHoten.Text = "";
            txtNgaysinh.Text = "";
            txtThangsinh.Text = "";
            txtNamsinh.Text = "";
            txtGioitinh.Text = "";
            txtDiachi.Text = "";
            txtMatinh.Text = "56";
            cboMabv.SelectedValue = "161";
            txtTenBV.Text = "Phßng kh¸m ®a khoa khu vùc 2";
            txtTenbenh.Text = "";
            txtDM.Value = 0;
            txtMau.Value = 0;
            txtDT.Value = 0;
            txtXN.Value = 0;
            txtCDHA.Value = 0;
            txtVanchuyen.Value = 0;
            txtTT.Value = 0;
            txtDVKT.Value = 0;
            txtTieuHao.Value = 0;
            txtThaythe.Value = 0;
            txtThuocK.Value = 0;
            txtTongtien.Value = 0;
            txtTienBHXH.Value = 0;
            txtTienBN.Value = 0;
            txtSophieu.Text = "";
            txtMathe.ReadOnly = false;
            txtCongkham.Value = 0;
            fa.Rows.Count = 1;
            EnableControlKCB(true);

        }
        private void GetKhoakham()
        {

            cboMakhoa.ClearItems();
            cboMakhoa.AllowSort = true;
            cboMakhoa.DataSource = p2hBUS.GetKhoaKham();
            cboMakhoa.Splits[0].DisplayColumns[0].Width = 50;
            cboMakhoa.Splits[0].DisplayColumns[1].Width = 400;

        }
        private void GetThuoc()
        {
            cboThuoc.ClearItems();
            cboThuoc.AllowSort = true;
            cboThuoc.DataSource = p2hBUS.GetTenHieu(Convert.ToInt32(Modules.GetKy().Rows[0]["thang"]),
               Convert.ToInt32(Modules.GetKy().Rows[0]["nam"]));
            cboThuoc.Splits[0].DisplayColumns[0].Width = 400;
            cboThuoc.Splits[0].DisplayColumns[1].Width = 50;
            cboThuoc.Splits[0].DisplayColumns[2].Width = 50;

        }
        private void GetMabv()
        {
            cboMabv.ClearItems();
            cboMabv.AllowSort = true;
            cboMabv.DataSource = p2hBUS.GetBV(txtMatinh.Text);
            cboMabv.Splits[0].DisplayColumns[0].Width = 50;
            cboMabv.Splits[0].DisplayColumns[1].Width = 200;


        }
        private void btnThoat_Click(object sender, EventArgs e)
        {
            if (flagGhi == true)
            {
                if (MessageBox.Show("®· thay ®æi th«ng tin b¹n cã muèn ghi kh«ng", "th«ng b¸o", MessageBoxButtons.YesNo) == System.Windows.Forms.DialogResult.Yes)
                {
                    btnGhi_Click(null, null);
                    flagGhi = false;
                }
            }
            this.Close();
        }
        private void SumNhomCP()
        {
            decimal decDM, decMau, decDC, decXN,
                decTNCN, decTT, decDVKTC, decVTYT,
                decTH, decNG, decVC, decVTYTTT, decThuocK;
            decDM = decMau = decDC = decXN =
            decTNCN = decTT = decDVKTC = decVTYT =
            decTH = decNG = decVC = decVTYTTT = decThuocK = 0;

            for (int i = 1; i <= fa.Rows.Count - 1; i++)
            {
                if (fa.Rows[i]["mahieu"] != null)
                {

                    switch (fa.Rows[i]["MaNhomCP"].ToString())
                    {
                        case "01":
                            decDM = decDM + Convert.ToDecimal(fa.Rows[i]["tongtien"]);
                            break;
                        case "02":
                            decMau = decMau + Convert.ToDecimal(fa.Rows[i]["tongtien"]);
                            break;
                        case "03":
                            decDC = decDC + Convert.ToDecimal(fa.Rows[i]["tongtien"]);
                            break;
                        case "04":
                            decXN = decXN + Convert.ToDecimal(fa.Rows[i]["tongtien"]);
                            break;
                        case "05":
                            decTNCN = decTNCN + Convert.ToDecimal(fa.Rows[i]["tongtien"]);
                            break;
                        case "06":
                        case "08":
                            decTT = decTT + Convert.ToDecimal(fa.Rows[i]["tongtien"]);
                            break;
                        case "09":
                            decDVKTC = decDVKTC + Convert.ToDecimal(fa.Rows[i]["tongtien"]);
                            break;
                        case "10":
                            decVTYT = decVTYT + Convert.ToDecimal(fa.Rows[i]["tongtien"]);
                            break;
                        case "11":
                            decNG = decNG + Convert.ToDecimal(fa.Rows[i]["tongtien"]);
                            break;
                        case "12":
                            decVC = decVC + Convert.ToDecimal(fa.Rows[i]["tongtien"]);
                            break;
                        case "13":
                            decVTYTTT = decVTYTTT + Convert.ToDecimal(fa.Rows[i]["tongtien"]);
                            break;
                        case "14":
                            decThuocK = decThuocK + Convert.ToDecimal(fa.Rows[i]["tongtien"]);
                            break;
                    }
                }
            }
            txtDM.Value = decDM;
            txtMau.Value = decMau;
            txtDT.Value = decDC;
            txtXN.Value = decXN;
            txtCDHA.Value = decTNCN;
            txtTT.Value = decTT;
            txtDVKT.Value = decDVKTC;
            txtTieuHao.Value = decVTYT;
            txtThaythe.Value = decVTYTTT;
            txtThuocK.Value = decThuocK;
            txtVanchuyen.Value = decVC;
            txtCongkham.Value = decNG;
            txtTongtien.Value = decDM + decMau + decDC + decXN +
                decTNCN + decTT + decDVKTC + decVTYT +
                decTH + decNG + decVC + decVTYTTT + decThuocK ;


        }
        private decimal Tyle()
        {
            if (cboDT.SelectedIndex != 0) { return 0; }
            if (txtMathe.Text.Length < txtMathe.MaxLength)
            {
                MessageBox.Show("Nhập lại số KCB");
                return 0;
            }
            if (cboDieutri.SelectedIndex == 1)
            {
                return Convert.ToDecimal(0.7);
            }
            if (Convert.ToDecimal(txtTongtien.Value) <= 172500)
            {
                return 1;
            }
            switch (txtMathe.Text.Substring(2, 1).ToString())
            {
                case "1":
                case "2":
                case "3":
                    return 1;
                case "4":
                case "5":
                    return Convert.ToDecimal(0.95);
                case "6":
                case "7":
                    return Convert.ToDecimal(0.8);

            }
            return 1;
        }
        private void GiaBHXH()
        {
            decimal decBHXH = 0;
            if (cboDT.SelectedIndex == 0)
            {
                if (txtMathe.Text.Length < txtMathe.MaxLength)
                {
                    MessageBox.Show("Nhập lại số KCB");
                    return;
                }
                for (int i = 1; i < fa.Rows.Count; i++)
                {
                    fa.Rows[i]["tienbhxh"] = Convert.ToInt32(fa.Rows[i]["SL"]) * Convert.ToDecimal(fa.Rows[i]["giabhxh"]) * Tyle();
                    decBHXH = decBHXH + Convert.ToDecimal(fa.Rows[i]["tienbhxh"]);
                    fa.Rows[i]["TienBN"] = Convert.ToDecimal(fa.Rows[i]["tongtien"]) - Convert.ToDecimal(fa.Rows[i]["tienbhxh"]);
                }

                txtTienBHXH.Value = decBHXH ;
                txtTienBN.Value = Convert.ToDecimal(txtTongtien.Value) - Convert.ToDecimal(txtTienBHXH.Value);
            }
            else //khong kham bang the bhyt
            {
                for (int i = 1; i < fa.Rows.Count; i++)
                {
                    fa.Rows[i]["TienBN"] = Convert.ToDecimal(fa.Rows[i]["tongtien"]);
                }
                txtTienBN.Value = Convert.ToDecimal(txtTongtien.Value);
            }

        }
        private void fa_AfterEdit(object sender, C1.Win.C1FlexGrid.RowColEventArgs e)
        {
            flagGhi = true;
            if (fa.Cols[e.Col].Name == "SL")
            {
                fa.Rows[e.Row]["tongtien"] = Convert.ToInt32(fa.Rows[e.Row]["SL"]) * Convert.ToDecimal(fa.Rows[e.Row]["gia"]);
                SumNhomCP();
                GiaBHXH();


            }
            if (fa.Cols[e.Col].Name == "TenCP" && fa.Rows[e.Row]["TenCP"].ToString().Trim() != "")
            {
                for (int i = 1; i < fa.Rows.Count; i++)
                {
                    if (e.Row != i && fa.Rows[i]["mahieu"] != null && fa.Rows[e.Row]["TenCP"].ToString().Trim() == fa.Rows[i]["TenCP"].ToString().Trim())
                    {
                        MessageBox.Show("Mã hiệu bị trùng vui lòng nhập lại");
                        fa.Rows[e.Row]["TenCP"] = "";
                        return;
                    }

                }
                fa.Rows[e.Row]["gia"] = cboThuoc.Columns["gia"].Value;
                fa.Rows[e.Row]["giabhxh"] = cboThuoc.Columns["giabhxh"].Value;
                fa.Rows[e.Row]["MaNhomCP"] = cboThuoc.Columns["sttnh"].Value;
                fa.Rows[e.Row]["mahieu"] = cboThuoc.Columns["mahieu"].Value;
                fa.Rows[e.Row]["ktc"] = cboThuoc.Columns["ktc"].Value;
                fa.Rows[e.Row]["dvt"] = cboThuoc.Columns["dvt"].Value;
                if (fa.Rows[e.Row]["SL"] != null)
                {
                    fa.Rows[e.Row]["tongtien"] = Convert.ToInt32(fa.Rows[e.Row]["SL"]) * Convert.ToDecimal(fa.Rows[e.Row]["gia"]);
                    SumNhomCP();
                    GiaBHXH();
                }
                switch (fa.Rows[e.Row]["MaNhomCP"].ToString())
                {
                    case "01":
                        fa.Rows[e.Row]["NhomCP"] = "Trong danh mục";
                        break;
                    case "02":
                        fa.Rows[e.Row]["NhomCP"] = "Máu, Đạm";
                        break;
                    case "03":
                        fa.Rows[e.Row]["NhomCP"] = "Dịch truyền";
                        break;
                    case "04":
                        fa.Rows[e.Row]["NhomCP"] = "Xét nghiệm";
                        break;
                    case "05":
                        fa.Rows[e.Row]["NhomCP"] = "CĐHA, TDCN";
                        break;
                    case "06":
                    case "08":
                        fa.Rows[e.Row]["NhomCP"] = "Thủ thuật";
                        break;
                    case "09":
                        fa.Rows[e.Row]["NhomCP"] = "DVKT cao";
                        break;
                    case "10":
                        fa.Rows[e.Row]["NhomCP"] = "VTYT tiêu hao";
                        break;
                    case "11":
                        fa.Rows[e.Row]["NhomCP"] = "Ngày giường";
                        break;
                    case "12":
                        fa.Rows[e.Row]["NhomCP"] = "Vận chuyển";
                        break;
                    case "13":
                        fa.Rows[e.Row]["NhomCP"] = "VTYT thay thế";
                        break;
                    case "14":
                        fa.Rows[e.Row]["NhomCP"] = "Thuốc K";
                        break;
                }
            }
        }
        private void btnGhi_Click(object sender, EventArgs e)
        {
            this.Cursor = Cursors.WaitCursor;
            if (txtMathe.ReadOnly == false)
            {
                if (InsertKCB() == false)
                {
                    MessageBox.Show("Vui Long Xem lai du lieu nhap");
                    this.Cursor = Cursors.Default;
                    return;
                }
            }
            else
            {
                if (UpdateKCB() == false)
                {
                    MessageBox.Show("Vui Long Xem lai du lieu nhap");
                    this.Cursor = Cursors.Default;
                    return;
                } 
            }
            flagGhi = false;
            lblthongbao.Text = "Lưu thông tin thành công";
            timer1.Enabled = true;
            this.Cursor = Cursors.Default;
        }
        private Boolean UpdateKCB()
        {
            if (p2hBUS.CheckMaTheTheoNgay(txtNgayTT.Text.ToString().Substring(6, 4) + "/" +
                txtNgayTT.Text.ToString().Substring(3, 2) + "/"
                + txtNgayTT.Text.ToString().Substring(0, 2), p2hVO.Mathe, p2hVO.Sophieu))
            {
                if (MessageBox.Show("M· thÎ nµy ®· kh¸m 2 lÇn trong ngµy, b¹n muèn tiÕp tôc kh«ng", "th«ng b¸o", MessageBoxButtons.YesNo) == System.Windows.Forms.DialogResult.No)
                {
                    return false;
                }
            }
            if (DateTime.Compare(Convert.ToDateTime(txtNgayvao.Value), Convert.ToDateTime(txtGTTU.Value)) < 0)
            {
                MessageBox.Show("ThÎ BHYT ®· hÕt h¹n", "th«ng b¸o");
                return false;
            }
            if (DateTime.Compare(Convert.ToDateTime(txtGTDen.Value), Convert.ToDateTime(txtNgayvao.Value)) < 0)
            {
                MessageBox.Show("ThÎ BHYT ®· hÕt h¹n", "th«ng b¸o");
                return false;
            }
            if (CheckSoLuongTon() == false) { return false; }
            SetValueToP2H(sttVO);
            SetValueToP2DU(sttVO);
            CheckUpdateP2D();
            if (p2hBUS.UpdateRecord(p2hVO, lstP2duVO, lstP2dVO) != null)
            {
                UpdateListP1DVO();
                GetThuoc();
                UpdateFA();
                return true;
            }
            else { return false; }
        }
        private void UpdateListP1DVO()
        {
            int count = lstP2dVO.Count;
            int i = 0;
            while (i < count)
            {
                if (lstP2dVO[i].Flag == 0)
                {
                    lstP2dVO.RemoveAt(i);
                    count--;
                }
                else
                {
                    lstP2dVO[i].Flag = 0;
                    i++;
                }

            }

        }
        private Boolean InsertKCB()
        {
            p2hVO = new P2hVO();
            if (DateTime.Compare(Convert.ToDateTime(txtNgayvao.Value), Convert.ToDateTime(txtGTTU.Value)) < 0)
            {
                MessageBox.Show("ThÎ BHYT ®· hÕt h¹n", "th«ng b¸o");
                return false;
            }
            if (DateTime.Compare(Convert.ToDateTime(txtGTDen.Value), Convert.ToDateTime(txtNgayvao.Value)) < 0)
            {
                MessageBox.Show("ThÎ BHYT ®· hÕt h¹n", "th«ng b¸o");
                return false;
            }
            if (CheckSoLuongTon() == false) { return false; }
            sttVO = sttDAO.GetSttNoi(Modules.nam);
            SetValueToP2H(sttVO);
            SetValueToP2DU(sttVO);
            SetValueToP2D(sttVO);
            if (p2hBUS.InsertRecord(p2hVO, lstP2duVO, lstP2dVO) != null)
            {
                sttDAO.UpdateSttNoi(Modules.nam, sttVO);
                txtSophieu.Text = string.Format("{0:000000}", sttVO.Phieung);
                GetThuoc();
                UpdateFA();
                txtMathe.ReadOnly = true;
                return true;
            }
            else { return false; }

        }
        private void InsertNoiTruKCB()
        {
            p2hVO = new P2hVO();
            if (CheckSoLuongTon() == false) { return; }
            sttVO = sttDAO.GetSttNoi(Modules.nam);
            SetValueToP2H(sttVO);
            SetValueToP2DU(sttVO);
            SetValueToP2D(sttVO);
            p2hBUS.InsertRecord(p2hVO, lstP2duVO, lstP2dVO);
            sttDAO.UpdateSttNoi(Modules.nam, sttVO);
            txtSophieu.Text = string.Format("{0:000000}", sttVO.Phieung);
            GetThuoc();
            UpdateFA();
            txtMathe.ReadOnly = true;
        }
        private void UpdateFA()
        {
            for (int i = 1; i < fa.Rows.Count; i++)
            {
                if (fa.Rows[i]["mahieu"] != null)
                {
                    fa.Rows[i]["slcu"] = fa.Rows[i]["sl"];
                }
            }
        }
        private bool CheckSoLuongTon()
        {
            DataTable tbPhieuNhap;
            string condition;
            for (int i = 1; i < fa.Rows.Count; i++)
            {
                if (fa.Rows[i]["mahieu"] != null && (fa.Rows[i]["manhomcp"].ToString() == "01" || fa.Rows[i]["manhomcp"].ToString() == "03"))
                {
                    condition = " and a.ma_hieu = '" + fa.Rows[i]["mahieu"].ToString() + "'";
                    tbPhieuNhap = phieunhapBUS.GetALL(Convert.ToInt32(Modules.thang), Modules.nam, condition);
                    if (tbPhieuNhap.Rows.Count == 0) { return false; }
                    if (Convert.ToInt32(tbPhieuNhap.Rows[0]["slcuoiky"]) + Convert.ToInt16(fa.Rows[i]["slcu"]) < Convert.ToInt16(fa.Rows[i]["sl"]))
                    {
                        MessageBox.Show("Số lượng thuốc " + fa.Rows[i]["TenCP"].ToString() + " còn tồn " + tbPhieuNhap.Rows[0]["slcuoiky"]);
                        return false;
                    }


                }
            }
            return true;
        }
        private void SetValueToP2D(SttVO sttVO)
        {
            P2dVO p2dVO;
            for (int i = 1; i < fa.Rows.Count; i++)
            {
                if (fa.Rows[i]["mahieu"] != null)
                {

                    p2dVO = new P2dVO();
                    p2dVO.Ctid = string.Format("{0:000000000000000}", sttVO.Ctid);
                    p2dVO.Ctid0 = string.Format("{0:000000000000000}", sttVO.Ctid0);
                    p2dVO.Sttnh = fa.Rows[i]["manhomcp"].ToString();
                    p2dVO.Mahieu = fa.Rows[i]["mahieu"].ToString();
                    p2dVO.Tenhieu = fa.Rows[i]["tencp"].ToString();
                    p2dVO.Tyle = 100;
                    p2dVO.Ktc = fa.Rows[i]["ktc"].ToString();
                    p2dVO.Sl = Convert.ToInt16(fa.Rows[i]["sl"]);
                    p2dVO.Gia = Convert.ToDecimal(fa.Rows[i]["gia"]);
                    p2dVO.Giabhxh = Convert.ToDecimal(fa.Rows[i]["giabhxh"]);
                    p2dVO.Tongtien = Convert.ToDecimal(fa.Rows[i]["tongtien"]);
                    p2dVO.Tienbhxh = Convert.ToDecimal(fa.Rows[i]["Tienbhxh"]);
                    p2dVO.Tienchenhlech = 0;//Convert.ToInt16(fa.Rows[i]["Tienchenhlech"]);
                    p2dVO.Tien_bn = Convert.ToDecimal(fa.Rows[i]["tienbn"]);
                    p2dVO.Dvt = fa.Rows[i]["dvt"].ToString();
                    p2dVO.Loai = 1;
                    p2dVO.Bacsy = "";
                    p2dVO.Khoa = "";
                    p2dVO.Chon = 0;
                    p2dVO.Flag = 0;
                    lstP2dVO.Add(p2dVO);
                    sttVO.Ctid0++;
                }
            }

        }
        private void CheckUpdateP2D()
        {
            P2dVO p2dVO;
            bool flag = false;
            decimal intCtid0;
            intCtid0 = sttDAO.GetStt_Ct_id2_Noi(Modules.nam, fa.Rows.Count - 1);
            for (int i = 1; i < fa.Rows.Count; i++)
            {
                if (fa.Rows[i]["mahieu"] != null &&
                    fa.Rows[i]["mahieu"].ToString() != "" && fa.Rows[i]["tencp"] != ""
                    && fa.Rows[i]["sl"] != null)
                {
                    flag = false;
                    foreach (P2dVO item in lstP2dVO)
                    {
                        //so khong thay doi
                        if (item.Mahieu == fa.Rows[i]["mahieu"].ToString())
                        {
                            if (item.Sl == Convert.ToInt16(fa.Rows[i]["sl"]))
                            {
                                item.Flag = 2;
                                item.Tienbhxh = Convert.ToDecimal(fa.Rows[i]["Tienbhxh"]);
                                item.Tien_bn = Convert.ToDecimal(fa.Rows[i]["tienbn"]);
                                flag = true;
                                break;
                            }
                            else //so luong thay doi
                            {

                                item.Sl = Convert.ToInt16(fa.Rows[i]["sl"]);
                                item.Tongtien = Convert.ToDecimal(fa.Rows[i]["tongtien"]);
                                item.Tienbhxh = Convert.ToDecimal(fa.Rows[i]["Tienbhxh"]);
                                item.Tienchenhlech = 0;//Convert.ToInt16(fa.Rows[i]["Tienchenhlech"]);
                                item.Tien_bn = Convert.ToDecimal(fa.Rows[i]["tienbn"]);
                                item.Flag = 2;
                                flag = true;
                                break;
                            }
                        }
                    }
                    // them moi du lieu
                    if (flag == false)
                    {
                        p2dVO = new P2dVO();
                        p2dVO.Ctid = string.Format("{0:000000000000000}", sttVO.Ctid);
                        p2dVO.Ctid0 = string.Format("{0:000000000000000}", intCtid0);
                        p2dVO.Sttnh = fa.Rows[i]["manhomcp"].ToString();
                        p2dVO.Mahieu = fa.Rows[i]["mahieu"].ToString();
                        p2dVO.Tenhieu = fa.Rows[i]["tencp"].ToString();
                        p2dVO.Tyle = 100;
                        p2dVO.Ktc = fa.Rows[i]["ktc"].ToString();
                        p2dVO.Sl = Convert.ToInt16(fa.Rows[i]["sl"]);
                        p2dVO.Gia = Convert.ToDecimal(fa.Rows[i]["gia"]);
                        p2dVO.Giabhxh = Convert.ToDecimal(fa.Rows[i]["giabhxh"]);
                        p2dVO.Tongtien = Convert.ToDecimal(fa.Rows[i]["tongtien"]);
                        p2dVO.Tienbhxh = Convert.ToDecimal(fa.Rows[i]["Tienbhxh"]);
                        p2dVO.Tienchenhlech = 0;//Convert.ToInt16(fa.Rows[i]["Tienchenhlech"]);
                        p2dVO.Tien_bn = Convert.ToDecimal(fa.Rows[i]["tienbn"]);
                        p2dVO.Dvt = fa.Rows[i]["dvt"].ToString();
                        p2dVO.Loai = 1;
                        p2dVO.Bacsy = "";
                        p2dVO.Khoa = "";
                        p2dVO.Chon = 0;
                        p2dVO.Flag = 3;
                        lstP2dVO.Add(p2dVO);
                        sttVO.Ctid0++;
                        intCtid0++;
                    }


                }

            }
        }
        private void SetValueToP2DU(SttVO sttVO)
        {
            lstP2duVO = new List<P2duVO>(11);
            lstP2duVO.Clear();
            P2duVO p2hVOTam = new P2duVO();
            lstP2duVO.Add(p2hVOTam);
            lstP2duVO[0].Sttnh = "01";

            lstP2duVO[0].Ctid = string.Format("{0:000000000000000}", sttVO.Ctid);
            lstP2duVO[0].Ctid2 = string.Format("{0:000000000000000}", sttVO.Ctid2);
            lstP2duVO[0].Tongtien = Convert.ToDecimal(txtDM.Value);
            lstP2duVO[0].Tienbn = Convert.ToDecimal(txtDM.Value) * (1 - Tyle());

            p2hVOTam = new P2duVO();
            lstP2duVO.Add(p2hVOTam);
            lstP2duVO[1].Sttnh = "02";
            lstP2duVO[1].Ctid = string.Format("{0:000000000000000}", sttVO.Ctid);
            lstP2duVO[1].Ctid2 = string.Format("{0:000000000000000}", sttVO.Ctid2 + 1);
            lstP2duVO[1].Tongtien = Convert.ToDecimal(txtMau.Value);
            lstP2duVO[1].Tienbn = Convert.ToDecimal(txtMau.Value) * (1 - Tyle());

            p2hVOTam = new P2duVO();
            lstP2duVO.Add(p2hVOTam);
            lstP2duVO[2].Sttnh = "03";
            lstP2duVO[2].Ctid = string.Format("{0:000000000000000}", sttVO.Ctid);
            lstP2duVO[2].Ctid2 = string.Format("{0:000000000000000}", sttVO.Ctid2 + 2);
            lstP2duVO[2].Tongtien = Convert.ToDecimal(txtDT.Value);
            lstP2duVO[2].Tienbn = Convert.ToDecimal(txtDT.Value) * (1 - Tyle());

            p2hVOTam = new P2duVO();
            lstP2duVO.Add(p2hVOTam);
            lstP2duVO[3].Sttnh = "04";
            lstP2duVO[3].Ctid = string.Format("{0:000000000000000}", sttVO.Ctid);
            lstP2duVO[3].Ctid2 = string.Format("{0:000000000000000}", sttVO.Ctid2 + 3);
            lstP2duVO[3].Tongtien = Convert.ToDecimal(txtXN.Value);
            lstP2duVO[3].Tienbn = Convert.ToDecimal(txtXN.Value) * (1 - Tyle());

            p2hVOTam = new P2duVO();
            lstP2duVO.Add(p2hVOTam);
            lstP2duVO[4].Sttnh = "05";
            lstP2duVO[4].Ctid = string.Format("{0:000000000000000}", sttVO.Ctid);
            lstP2duVO[4].Ctid2 = string.Format("{0:000000000000000}", sttVO.Ctid2 + 4);
            lstP2duVO[4].Tongtien = Convert.ToDecimal(txtCDHA.Value);
            lstP2duVO[4].Tienbn = Convert.ToDecimal(txtCDHA.Value) * (1 - Tyle());

            p2hVOTam = new P2duVO();
            lstP2duVO.Add(p2hVOTam);
            lstP2duVO[5].Sttnh = "06";
            lstP2duVO[5].Ctid = string.Format("{0:000000000000000}", sttVO.Ctid);
            lstP2duVO[5].Ctid2 = string.Format("{0:000000000000000}", sttVO.Ctid2 + 5);
            lstP2duVO[5].Tongtien = 0;
            lstP2duVO[5].Tienbn = 0;

           
            p2hVOTam = new P2duVO();
            lstP2duVO.Add(p2hVOTam);
            lstP2duVO[6].Sttnh = "08";
            lstP2duVO[6].Ctid = string.Format("{0:000000000000000}", sttVO.Ctid);
            lstP2duVO[6].Ctid2 = string.Format("{0:000000000000000}", sttVO.Ctid2 + 6);
            lstP2duVO[6].Tongtien = Convert.ToDecimal(txtTT.Value);
            lstP2duVO[6].Tienbn = Convert.ToDecimal(txtTT.Value) * (1 - Tyle());

            p2hVOTam = new P2duVO();
            lstP2duVO.Add(p2hVOTam);
            lstP2duVO[7].Sttnh = "09";
            lstP2duVO[7].Ctid = string.Format("{0:000000000000000}", sttVO.Ctid);
            lstP2duVO[7].Ctid2 = string.Format("{0:000000000000000}", sttVO.Ctid2 + 7);
            lstP2duVO[7].Tongtien = Convert.ToDecimal(txtDVKT.Value);
            lstP2duVO[7].Tienbn = Convert.ToDecimal(txtDVKT.Value) * (1 - Tyle());

            p2hVOTam = new P2duVO();
            lstP2duVO.Add(p2hVOTam);
            lstP2duVO[8].Sttnh = "10";
            lstP2duVO[8].Ctid = string.Format("{0:000000000000000}", sttVO.Ctid);
            lstP2duVO[8].Ctid2 = string.Format("{0:000000000000000}", sttVO.Ctid2 + 8);
            lstP2duVO[8].Tongtien = Convert.ToDecimal(txtTieuHao.Value);
            lstP2duVO[8].Tienbn = Convert.ToDecimal(txtTieuHao.Value) * (1 - Tyle());

            p2hVOTam = new P2duVO();
            lstP2duVO.Add(p2hVOTam);
            lstP2duVO[9].Sttnh = "11";
            lstP2duVO[9].Ctid = string.Format("{0:000000000000000}", sttVO.Ctid);
            lstP2duVO[9].Ctid2 = string.Format("{0:000000000000000}", sttVO.Ctid2 + 9);
            lstP2duVO[9].Tongtien = Convert.ToDecimal(txtCongkham.Value);
            lstP2duVO[9].Tienbn = Convert.ToDecimal(txtCongkham.Value) * (1 - Tyle());

            p2hVOTam = new P2duVO();
            lstP2duVO.Add(p2hVOTam);
            lstP2duVO[10].Sttnh = "12";
            lstP2duVO[10].Ctid = string.Format("{0:000000000000000}", sttVO.Ctid);
            lstP2duVO[10].Ctid2 = string.Format("{0:000000000000000}", sttVO.Ctid2 + 10);
            lstP2duVO[10].Tongtien = Convert.ToDecimal(txtVanchuyen.Value);
            lstP2duVO[10].Tienbn = Convert.ToDecimal(txtVanchuyen.Value) * (1 - Tyle());

            p2hVOTam = new P2duVO();
            lstP2duVO.Add(p2hVOTam);
            lstP2duVO[11].Sttnh = "13";
            lstP2duVO[11].Ctid = string.Format("{0:000000000000000}", sttVO.Ctid);
            lstP2duVO[11].Ctid2 = string.Format("{0:000000000000000}", sttVO.Ctid2 + 11);
            lstP2duVO[11].Tongtien = Convert.ToDecimal(txtThaythe.Value);
            lstP2duVO[11].Tienbn = Convert.ToDecimal(txtThaythe.Value) * (1 - Tyle());

            p2hVOTam = new P2duVO();
            lstP2duVO.Add(p2hVOTam);
            lstP2duVO[12].Sttnh = "14";
            lstP2duVO[12].Ctid = string.Format("{0:000000000000000}", sttVO.Ctid);
            lstP2duVO[12].Ctid2 = string.Format("{0:000000000000000}", sttVO.Ctid2 + 12);
            lstP2duVO[12].Tongtien = Convert.ToDecimal(txtThuocK.Value);
            lstP2duVO[12].Tienbn = Convert.ToDecimal(txtThuocK.Value) * (1 - Tyle());

        }
        private void SetValueToP2H(SttVO sttVO)
        {
            p2hVO.Ctid = string.Format("{0:000000000000000}", sttVO.Ctid);
            p2hVO.Loai = "01";
            p2hVO.Matinh = "56";
            p2hVO.Sophieu = string.Format("{0:000000}", sttVO.Phieung);
            p2hVO.Benhvien = "161";
            p2hVO.Makhoa = cboMakhoa.Columns[0].Value.ToString();
            p2hVO.Tenkhoa = cboMakhoa.Columns[1].Value.ToString();
            p2hVO.Mathe = txtMathe.Text;
            p2hVO.Namphah = "";
            if (txtMathe.Text != "")
            {
                p2hVO.Madt = txtMathe.Text.ToString().Substring(0, 2);
                p2hVO.Loaitt = txtMathe.Text.ToString().Substring(2, 1);
                p2hVO.Mabhxh = txtMathe.Text.Substring(3, 2);
                p2hVO.Vanchuyen = vanchuyen;
            }
            else
            {
                p2hVO.Madt = "";
                p2hVO.Loaitt = "";
                p2hVO.Mabhxh = "";
                p2hVO.Vanchuyen = "";
            }

            p2hVO.Doituong = Convert.ToInt16(cboDT.Text.ToString().Substring(0, 1));
            p2hVO.Loaitt1 = 1;
            p2hVO.Hoten = txtHoten.Text;
            p2hVO.Gioitinh = Convert.ToInt16(txtGioitinh.Text);
            p2hVO.Ngaysinh = Convert.ToInt32(txtNgaysinh.Text);
            p2hVO.Thangsinh = Convert.ToInt32(txtThangsinh.Text);
            p2hVO.Namsinh = Convert.ToInt32(txtNamsinh.Text);
            p2hVO.Diachi = txtDiachi.Text;
            p2hVO.Tinhkcb = txtMatinh.Text;
            p2hVO.Makcb = cboMabv.SelectedValue.ToString();
            p2hVO.Noikham = txtTenBV.Text;
            p2hVO.Ngayvao = txtNgayvao.Text.ToString().Substring(6, 4) + "/" + txtNgayvao.Text.ToString().Substring(3, 2) + "/" + txtNgayvao.Text.ToString().Substring(0, 2);
            p2hVO.Ngayra = txtNgayra.Text.ToString().Substring(6, 4) + "/" + txtNgayra.Text.ToString().Substring(3, 2) + "/" + txtNgayra.Text.ToString().Substring(0, 2);
            p2hVO.Gttu = txtGTTU.Text.ToString().Substring(6, 4) + "/" + txtGTTU.Text.ToString().Substring(3, 2) + "/" + txtGTTU.Text.ToString().Substring(0, 2);
            p2hVO.Gtden = txtGTDen.Text.ToString().Substring(6, 4) + "/" + txtGTDen.Text.ToString().Substring(3, 2) + "/" + txtGTDen.Text.ToString().Substring(0, 2);
            p2hVO.Ngaytt = txtNgayTT.Text.ToString().Substring(6, 4) + "/" + txtNgayTT.Text.ToString().Substring(3, 2) + "/" + txtNgayTT.Text.ToString().Substring(0, 2);
            p2hVO.Tenbenh = txtTenbenh.Text;
            p2hVO.Maicd = cboICD.SelectedValue.ToString();
            p2hVO.Noiden = txtNcd.Text;
            p2hVO.Ngay1 = txtNgayTT.Text.ToString().Substring(6, 4) + "/" + txtNgayTT.Text.ToString().Substring(3, 2) + "/" + txtNgayTT.Text.ToString().Substring(0, 2);
            p2hVO.Ngay2 = txtNgayTT.Text.ToString().Substring(6, 4) + "/" + txtNgayTT.Text.ToString().Substring(3, 2) + "/" + txtNgayTT.Text.ToString().Substring(0, 2);
            p2hVO.Ngay3 = txtNgayTT.Text.ToString().Substring(6, 4) + "/" + txtNgayTT.Text.ToString().Substring(3, 2) + "/" + txtNgayTT.Text.ToString().Substring(0, 2);
            p2hVO.Thaisan = 0;
            p2hVO.Ngaylv = txtNgayvao.Text.ToString().Substring(6, 4) + "/" + txtNgayvao.Text.ToString().Substring(3, 2) + "/" + txtNgayvao.Text.ToString().Substring(0, 2);
            p2hVO.Usernam = Properties.Settings.Default.UserName;
            p2hVO.Excel = 0;
            p2hVO.Dieutri = cboDieutri.Text.Substring(0, 1);
            p2hVO.Ketqua = 0;
            p2hVO.Benhphu = "";
            p2hVO.Dain = "";
           
            p2hVO.Benhan = "";
            p2hVO.Lenhdx = "";
            p2hVO.Madv = "";
            p2hVO.Macb = "";

        }
        public void SetValueToControl()
        {
            sttVO = new SttVO();
            sttVO.Ctid = Convert.ToDecimal(p2hVO.Ctid);
            sttVO.Phieung = Convert.ToDecimal(p2hVO.Sophieu);
            txtSophieu.Text = p2hVO.Sophieu;
            cboMakhoa.SelectedValue = p2hVO.Makhoa;
            txtMathe.Text = p2hVO.Mathe.Trim();
            txtMathe.ReadOnly = true;
            vanchuyen = p2hVO.Vanchuyen;
            txtHoten.Text = p2hVO.Hoten.Trim(); ;
            txtGioitinh.Text = p2hVO.Gioitinh.ToString();
            txtNgaysinh.Text = p2hVO.Ngaysinh.ToString();
            txtThangsinh.Text = p2hVO.Thangsinh.ToString();
            txtNamsinh.Text = p2hVO.Namsinh.ToString();
            txtDiachi.Text = p2hVO.Diachi.Trim();
            txtMatinh.Text = p2hVO.Tinhkcb;
            txtMatinh_Leave(null, null);
            cboMabv.SelectedValue = p2hVO.Makcb;
            cboICD.SelectedValue = p2hVO.Maicd;
            txtTenBV.Text = p2hVO.Noikham.Trim();
            txtNgayvao.Value = p2hVO.Ngayvao;
            txtNgayra.Value = p2hVO.Ngayra;
            txtGTTU.Value = p2hVO.Gttu;
            txtGTDen.Value = p2hVO.Gtden;
            txtNgayTT.Value = p2hVO.Ngaytt;
            txtTenbenh.Text = p2hVO.Tenbenh.Trim();
            txtNcd.Text = p2hVO.Noiden.Trim();
            txtTenbenh.Focus();
            if (p2hVO.Doituong >= 3)
            {
                cboDT.SelectedIndex = Convert.ToInt16(p2hVO.Doituong) - 2;
            }
            else
            {
                cboDT.SelectedIndex = Convert.ToInt16(p2hVO.Doituong) - 1;
            }

            if (p2hVO.Dieutri == " ")
            {
                cboDieutri.SelectedIndex = 0;
            }
            else
            {
                cboDieutri.SelectedIndex = Convert.ToInt16(p2hVO.Dieutri) + 1;
            }

        }
        public void SetValueToControlP2DU()
        {

            lstP2duVO = p2hBUS.GetListP2DU("p2_du", P2hVO.Ctid);
            sttVO.Ctid2 = Convert.ToDecimal(lstP2duVO[0].Ctid2);
            txtDM.Value = lstP2duVO[0].Tongtien;
            txtMau.Value = lstP2duVO[1].Tongtien;
            txtDT.Value = lstP2duVO[2].Tongtien;
            txtXN.Value = lstP2duVO[3].Tongtien;
            txtCDHA.Value = lstP2duVO[4].Tongtien;
            txtTT.Value = lstP2duVO[6].Tongtien;
            txtDVKT.Value = lstP2duVO[7].Tongtien;
            txtTieuHao.Value = lstP2duVO[8].Tongtien;
            txtCongkham.Value = lstP2duVO[9].Tongtien;
            txtVanchuyen.Value = lstP2duVO[10].Tongtien;
            txtThaythe.Value = lstP2duVO[11].Tongtien;
            txtThuocK.Value = lstP2duVO[12].Tongtien;

           
           
           

        }
        public void SetValueToControlP2D()
        {

            lstP2dVO = p2hBUS.GetListP2D("p2_d", P2hVO.Ctid);
            fa.Rows.Count = 1;
            decimal decBHXH = 0;
            int i = 1;
            foreach (P2dVO p2dvo in lstP2dVO)
            {
                fa.Rows.Count = fa.Rows.Count + 1;
                fa.Rows[i]["manhomcp"] = p2dvo.Sttnh;
                fa.Rows[i]["tencp"] = p2dvo.Tenhieu.Trim();
                fa.Rows[i]["mahieu"] = p2dvo.Mahieu;
                fa.Rows[i]["ktc"] = p2dvo.Ktc;
                fa.Rows[i]["sl"] = p2dvo.Sl;
                fa.Rows[i]["slcu"] = p2dvo.Sl;
                fa.Rows[i]["gia"] = p2dvo.Gia;
                fa.Rows[i]["giabhxh"] = p2dvo.Giabhxh;
                fa.Rows[i]["tongtien"] = p2dvo.Tongtien;
                fa.Rows[i]["Tienbhxh"] = p2dvo.Tienbhxh;
                fa.Rows[i]["dvt"] = p2dvo.Dvt;
                decBHXH = decBHXH + Convert.ToDecimal(fa.Rows[i]["tienbhxh"]);
                fa.Rows[i]["tienbn"] = p2dvo.Tien_bn;
                switch (fa.Rows[i]["MaNhomCP"].ToString())
                {
                    case "01":
                        fa.Rows[i]["NhomCP"] = "Trong danh mục";
                        break;
                    case "02":
                        fa.Rows[i]["NhomCP"] = "Máu, Đạm";
                        break;
                    case "03":
                        fa.Rows[i]["NhomCP"] = "Dịch truyền";
                        break;
                    case "04":
                        fa.Rows[i]["NhomCP"] = "Xét nghiệm";
                        break;
                    case "05":
                        fa.Rows[i]["NhomCP"] = "CĐHA, TDCN";
                        break;
                    case "06":
                    case "08":
                        fa.Rows[i]["NhomCP"] = "Thủ thuật";
                        break;
                    case "09":
                        fa.Rows[i]["NhomCP"] = "DVKT cao";
                        break;
                    case "10":
                        fa.Rows[i]["NhomCP"] = "VTYT tiêu hao";
                        break;
                    case "11":
                        fa.Rows[i]["NhomCP"] = "Ngày giường";
                        break;
                    case "12":
                        fa.Rows[i]["NhomCP"] = "Vận chuyển";
                        break;
                    case "13":
                        fa.Rows[i]["NhomCP"] = "VTYT thay thế";
                        break;
                    case "14":
                        fa.Rows[i]["NhomCP"] = "Thuốc K";
                        break;
                }
                i++;

            }
            txtDM.Value = lstP2duVO[0].Tongtien;
            txtMau.Value = lstP2duVO[1].Tongtien;
            txtDT.Value = lstP2duVO[2].Tongtien;
            txtXN.Value = lstP2duVO[3].Tongtien;
            txtCDHA.Value = lstP2duVO[4].Tongtien;
            txtTT.Value = lstP2duVO[6].Tongtien;
            txtDVKT.Value = lstP2duVO[7].Tongtien;
            txtTieuHao.Value = lstP2duVO[8].Tongtien;
            txtCongkham.Value = lstP2duVO[9].Tongtien;
            txtVanchuyen.Value = lstP2duVO[10].Tongtien;
            txtThaythe.Value = lstP2duVO[11].Tongtien;
            txtThuocK.Value = lstP2duVO[12].Tongtien;

            txtTongtien.Value = Convert.ToDecimal(txtDM.Value) + Convert.ToDecimal(txtMau.Value)
                + Convert.ToDecimal(txtDT.Value) + Convert.ToDecimal(txtXN.Value)
                + Convert.ToDecimal(txtCDHA.Value) + Convert.ToDecimal(txtCongkham.Value) + Convert.ToDecimal(txtTT.Value)
                + Convert.ToDecimal(txtDVKT.Value) + Convert.ToDecimal(txtTieuHao.Value)
                + Convert.ToDecimal(txtVanchuyen.Value) + Convert.ToDecimal(txtThaythe.Value)
                + Convert.ToDecimal(txtThuocK.Value);
            txtTienBHXH.Value = decBHXH;
            txtTienBN.Value = Convert.ToDecimal(txtTongtien.Value) - Convert.ToDecimal(txtTienBHXH.Value);

        }
        private void fa_AfterDeleteRow(object sender, C1.Win.C1FlexGrid.RowColEventArgs e)
        {
            flagGhi = true;
            SumNhomCP();
            GiaBHXH();
        }
        private void btnXoa_Click(object sender, EventArgs e)
        {
            if (MessageBox.Show("B¹n cã muèn xãa b¶ng kª nµy kh«ng", "th«ng b¸o", MessageBoxButtons.YesNo) == System.Windows.Forms.DialogResult.Yes)
            {
                p2hBUS.DeleteRecord(p2hVO.Ctid);
                ClearControl();
                GetThuoc();
                lblthongbao.Text = "Xóa thông tin thành công";
                timer1.Enabled = true;

            }
        }
        private string Muchuong(string loaitt)
        {
            if (loaitt == " ")
            {
                return "0%";
            }
            if (cboDieutri.SelectedIndex == 1)
            {
                return "70%";
            }
            switch (txtMathe.Text.Substring(2, 1).ToString())
            {
                case "1":
                case "2":
                case "3":
                    return "100%";
                case "4":
                case "5":
                    return "95%";
                case "6":
                case "7":
                    return "80%";

            }
            return "100%";
        }
        private void btnIn_Click(object sender, EventArgs e)
        {
            int stt = 0;
            string strTennhom = "cong kham";
            DataRow newRow = null;
            Dataset.dsBCNX dsBCNX = new Dataset.dsBCNX();
            newRow = dsBCNX.Tables["P1H"].NewRow();
            newRow["sophieu"] = P2hVO.Sophieu;
            newRow["muchuong"] = Muchuong(P2hVO.Loaitt);
            newRow["hoten"] = Converter.TCVN3ToUnicode(P2hVO.Hoten);
            newRow["namsinh"] = p2hVO.Ngaysinh + "/" + p2hVO.Thangsinh + "/" + p2hVO.Namsinh;
            newRow["lenhdx"] = Converter.TCVN3ToUnicode(p2hVO.Noiden);
            newRow["gioitinh"] = p2hVO.Gioitinh;
            newRow["diachi"] = Converter.TCVN3ToUnicode(p2hVO.Diachi);
            newRow["mathe"] = p2hVO.Mathe;
            newRow["gttu"] = Convert.ToDateTime(p2hVO.Gttu).ToString().Substring(0, 10);
            newRow["gtden"] = Convert.ToDateTime(p2hVO.Gtden).ToString().Substring(0, 10);
            newRow["makcb"] = p2hVO.Makcb;
            newRow["tinhkcb"] = p2hVO.Tinhkcb;
            newRow["noikham"] = Converter.TCVN3ToUnicode(p2hVO.Noikham);
            newRow["ngayvao"] = Convert.ToDateTime(p2hVO.Ngayvao).ToString().Substring(0, 10);
            newRow["ngayra"] = Convert.ToDateTime(p2hVO.Ngayra).ToString().Substring(0, 10);
            if ((Convert.ToDateTime(p2hVO.Ngayra) - Convert.ToDateTime(p2hVO.Ngayvao)).Days != 0)
            {
                newRow["tongngay"] = (Convert.ToDateTime(p2hVO.Ngayra) - Convert.ToDateTime(p2hVO.Ngayvao)).Days;
            }
            else
            {
                newRow["tongngay"] = 1;
            }
            newRow["ngaytt"] = Convert.ToDateTime(p2hVO.Ngaytt).ToString().Substring(0, 10);
            newRow["dieutri"] = p2hVO.Dieutri;
            newRow["chuandoan"] = Converter.TCVN3ToUnicode(p2hVO.Tenbenh);
            newRow["maicd"] = p2hVO.Maicd;
            if (p2hVO.Mathe != "")
            {
                newRow["SOKCB11"] = p2hVO.Mathe.Substring(0, 2);
                newRow["SOKCB12"] = p2hVO.Mathe.Substring(2, 1);
                newRow["SOKCB13"] = p2hVO.Mathe.Substring(3, 2);
                newRow["SOKCB14"] = p2hVO.Mathe.Substring(5, 2);
                newRow["SOKCB15"] = p2hVO.Mathe.Substring(7, 3);
                newRow["SOKCB16"] = p2hVO.Mathe.Substring(10);
            }
            newRow["tongtien"] = Modules.ChangeToCamel(Modules.So_chu(Convert.ToDouble(txtTongtien.Value)).Trim());
            newRow["Tienbhxh"] = Modules.ChangeToCamel(Modules.So_chu(Convert.ToDouble(txtTienBHXH.Value)).Trim());
            if (Convert.ToDouble(txtTienBN.Value) >= 1)
            {
                newRow["Tienbn"] = Modules.ChangeToCamel(Modules.So_chu(Convert.ToDouble(txtTienBN.Value)).Trim());
            }
            newRow["fullname"] = Properties.Settings.Default.FullName;
            newRow["ketoan"] = Properties.Settings.Default.Ketoan;
            newRow["doituong"] = p2hVO.Doituong;

            dsBCNX.Tables["P1H"].Rows.Add(newRow);

            //newRow = dsBCNX.Tables["P1D"].NewRow();
            //newRow["nhomcp"] = stt;
            //newRow["tencp"] = "";
            //newRow["dvt"] = "";
            //newRow["tongtien"] = Convert.ToDecimal(txtCongkham.Value);
            //newRow["Tienbhxh"] = Convert.ToDecimal(txtCongkham.Value) * Tyle();
            //newRow["tienbn"] = Convert.ToDecimal(newRow["tongtien"]) - Convert.ToDecimal(newRow["Tienbhxh"]);
            //newRow["tennhomcp"] = "Ngày giường chuyên khoa";

            //dsBCNX.Tables["P1D"].Rows.Add(newRow);
            var q = lstP2dVO.OrderBy(o => o.Sttnh);
            foreach (P2dVO p2dvo in q)
            {
                newRow = dsBCNX.Tables["P1D"].NewRow();
                if (p2dvo.Sttnh != strTennhom)
                {
                    stt++;
                    strTennhom = p2dvo.Sttnh;
                }
                newRow["nhomcp"] = stt;
                newRow["tencp"] = Converter.TCVN3ToUnicode(p2dvo.Tenhieu);
                newRow["dvt"] = p2dvo.Dvt;
                newRow["sl"] = p2dvo.Sl;
                newRow["gia"] = p2dvo.Gia;
                newRow["tongtien"] = p2dvo.Tongtien;
                newRow["Tienbhxh"] = p2dvo.Tienbhxh;
                newRow["tienbn"] = p2dvo.Tien_bn;
                switch (p2dvo.Sttnh.ToString())
                {
                    case "01":
                        newRow["tennhomcp"] = "Trong danh mục";
                        break;
                    case "02":
                        newRow["tennhomcp"] = "Máu, Đạm";
                        break;
                    case "03":
                        newRow["tennhomcp"] = "Dịch truyền";
                        break;
                    case "04":
                        newRow["tennhomcp"] = "Xét nghiệm";
                        break;
                    case "05":
                        newRow["tennhomcp"] = "CĐHA, TDCN";
                        break;
                    case "06":
                    case "08":
                        newRow["tennhomcp"] = "Thủ thuật";
                        break;
                    case "09":
                        newRow["tennhomcp"] = "DVKT cao";
                        break;
                    case "10":
                        newRow["tennhomcp"] = "VTYT tiêu hao";
                        break;
                    case "11":
                        newRow["tennhomcp"] = "Ngày giường";
                        break;
                    case "12":
                        newRow["tennhomcp"] = "Vận chuyển";
                        break;
                    case "13":
                        newRow["tennhomcp"] = "VTYT thay thế";
                        break;
                    case "14":
                        newRow["tennhomcp"] = "Thuốc K";
                        break;
                }

                dsBCNX.Tables["P1D"].Rows.Add(newRow);
            }
            Cursor.Current = Cursors.Default;
            Report f = new Report();
            ReportDocument cr = new ReportDocument();
            cr.Load("rptKCBNoitru.rpt");
            cr.SetDataSource(dsBCNX);
            f.crpView.ReportSource = cr;
            Button bt = (Button)sender;
            if (bt.Name == "btnIn")
            {
                f.crpView.PrintReport();
            }
            else
            {
                f.Show();
            }
        }
        private void btnTim_Click(object sender, EventArgs e)
        {
            if (flagGhi == true)
            {
                if (MessageBox.Show("®· thay ®æi th«ng tin b¹n cã muèn ghi kh«ng", "th«ng b¸o", MessageBoxButtons.YesNo) == System.Windows.Forms.DialogResult.Yes)
                {
                    btnGhi_Click(null, null);

                }
            }
            flagGhi = false;
            Cursor.Current = Cursors.WaitCursor;
            frmSearch childForm = (frmSearch)GetForm("frmSearch");
            if (childForm == null)
            {
                childForm = new frmSearch();
                childForm.MdiParent = this.Parent.FindForm();
                childForm.WindowState = FormWindowState.Maximized;
                childForm.Show();
                
            }
            else
            {
                childForm.Activate();
              
            }
            childForm.clear(1);
            Cursor.Current = Cursors.Default;
        }
        private Form GetForm(String FormName)
        {
            foreach (Form f in this.Parent.FindForm().MdiChildren)
            {
                if (f.Name == FormName)
                    return f;
            }
            return null;
        }
        private void txtHoten_Leave(object sender, EventArgs e)
        {
            txtHoten.Text = Modules.ChangeToCamel(txtHoten.Text);
        }
        private void txtDiachi_Leave(object sender, EventArgs e)
        {
            txtDiachi.Text = Modules.ChangeToCamel(txtDiachi.Text);
        }

        private void cboMabv_SelectedValueChanged(object sender, EventArgs e)
        {
            txtTenBV.Text = cboMabv.Columns[1].Value.ToString();
        }

        private void cboMabv_Leave(object sender, EventArgs e)
        {
            if (cboMabv.SelectedValue != null)
            {
                if (cboMabv.SelectedValue.ToString() == "161")
                {
                    cboDieutri.SelectedIndex = 0;
                }
                else
                {
                    cboDieutri.SelectedIndex = 1;

                }
            }
        }

        private void cboDT_Leave(object sender, EventArgs e)
        {
            if (cboDT.SelectedIndex == 0)
            {
                EnableControlKCB(true);
                txtMathe.Focus();
            }
            else
            {
                EnableControlKCB(false);
                txtHoten.Focus();
            }
        }
        private void EnableControlKCB(bool flag)
        {
            txtMathe.Enabled = flag;
            cboDieutri.Enabled = flag;
            txtMatinh.Enabled = flag;
            cboMabv.Enabled = flag;
            txtGTTU.Enabled = flag;
            txtGTDen.Enabled = flag;
            txtTenBV.Enabled = flag;
        }

        private void fa_KeyUp(object sender, KeyEventArgs e)
        {
            if (fa.Selection.c1 == 4 && e.KeyCode == Keys.Enter)
            {
                fa.Select(fa.Selection.r1 + 1, 2);
                fa.StartEditing(fa.Selection.r1, 2);

            }
        }

        private void timer1_Tick(object sender, EventArgs e)
        {
            lblthongbao.Text = "";
            timer1.Enabled = false;
        }

        private void cboDieutri_Leave(object sender, EventArgs e)
        {
            for (int i = 1; i <= fa.Rows.Count - 1; i++)
            {
                fa.Rows[i]["tongtien"] = Convert.ToInt32(fa.Rows[i]["SL"]) * Convert.ToDecimal(fa.Rows[i]["gia"]);

            }
            SumNhomCP();
            GiaBHXH();
            txtHoten.Focus();
        }

        private void frmKCBNoitru_KeyDown(object sender, KeyEventArgs e)
        {
            Modules.SendKey(this, e);
            if (e.KeyCode == Keys.F11)
            {
                fa.Focus();
                fa.Select(fa.Rows.Count - 1, 2);
                fa.StartEditing(fa.Rows.Count - 1, 2);

            }
        }

        private void btnMucmoi_Click(object sender, EventArgs e)
        {
            if (txtMathe.ReadOnly == true)
            {
                Cursor.Current = Cursors.WaitCursor;
                frmKCBNoitruDetail childForm = new frmKCBNoitruDetail();
                childForm.p2hVO = p2hVO;
                childForm.sttVO = sttVO;
                childForm.btnThêm_Click(null, null);
                childForm.ShowDialog();
                SetValueToControlP2D_Detail();
                SumNhomCP();
                GiaBHXH();
                btnGhi_Click(null, null);
                Cursor.Current = Cursors.Default;
            }
            else
            {
                MessageBox.Show("Vui lßng nhËp th«ng tin KCB tr­íc khi nhËp thuèc theo ngµy");
            }
           
        }

        public void SetValueToControlP2D_Detail()
        {

           
            fa.Rows.Count = 1;
            decimal decBHXH = 0;
            int i = 1;
            List<P2dVO> lstP2d_detail = p2hBUS.GetListP2D_Detail(p2hVO.Ctid);
            foreach (P2dVO p2dvo in lstP2d_detail)
            {
                fa.Rows.Count = fa.Rows.Count + 1;
                fa.Rows[i]["manhomcp"] = p2dvo.Sttnh;
                fa.Rows[i]["tencp"] = p2dvo.Tenhieu.Trim();
                fa.Rows[i]["mahieu"] = p2dvo.Mahieu;
                fa.Rows[i]["ktc"] = p2dvo.Ktc;
                fa.Rows[i]["sl"] = p2dvo.Sl;
                fa.Rows[i]["slcu"] = p2dvo.Sl;
                fa.Rows[i]["gia"] = p2dvo.Gia;
                fa.Rows[i]["giabhxh"] = p2dvo.Giabhxh;
                fa.Rows[i]["tongtien"] = p2dvo.Tongtien;
                fa.Rows[i]["Tienbhxh"] = p2dvo.Tienbhxh;
                fa.Rows[i]["dvt"] = p2dvo.Dvt;
                decBHXH = decBHXH + Convert.ToDecimal(fa.Rows[i]["tienbhxh"]);
                fa.Rows[i]["tienbn"] = p2dvo.Tien_bn;
                switch (fa.Rows[i]["MaNhomCP"].ToString())
                {
                    case "01":
                        fa.Rows[i]["NhomCP"] = "Trong danh mục";
                        break;
                    case "02":
                        fa.Rows[i]["NhomCP"] = "Máu, Đạm";
                        break;
                    case "03":
                        fa.Rows[i]["NhomCP"] = "Dịch truyền";
                        break;
                    case "04":
                        fa.Rows[i]["NhomCP"] = "Xét nghiệm";
                        break;
                    case "05":
                        fa.Rows[i]["NhomCP"] = "CĐHA, TDCN";
                        break;
                    case "06":
                    case "08":
                        fa.Rows[i]["NhomCP"] = "Thủ thuật";
                        break;
                    case "09":
                        fa.Rows[i]["NhomCP"] = "DVKT cao";
                        break;
                    case "10":
                        fa.Rows[i]["NhomCP"] = "VTYT tiêu hao";
                        break;
                    case "11":
                        fa.Rows[i]["NhomCP"] = "Ngày giường";
                        break;
                    case "12":
                        fa.Rows[i]["NhomCP"] = "Vận chuyển";
                        break;
                    case "13":
                        fa.Rows[i]["NhomCP"] = "VTYT thay thế";
                        break;
                    case "14":
                        fa.Rows[i]["NhomCP"] = "Thuốc K";
                        break;
                }
                i++;

            }
            
        }

        private void txtMatinh_Leave(object sender, EventArgs e)
        {
            if (txtMatinh.Text != matinhtam)
            {
                GetMabv();
                matinhtam = txtMatinh.Text;
            }
        }
        private void docfiledbf()
        {
            string FileName = System.IO.Directory.GetCurrentDirectory().ToString() + "\\dmicd.dbf";
            string Path = FileName.Substring(0);
            int Separator = Path.LastIndexOf("\\");
            string DataSource = FileName.Substring(0, Separator) + "\\";

            string File = FileName.Substring(Separator);

            int Separator2 = File.LastIndexOf("\\");
            string DBF = File.Remove(Separator2, 1);

            int Separator3 = DBF.LastIndexOf(".");
            string DBF_Extension = DBF.Substring(Separator3);
            string DBF_FileName = DBF.Remove(Separator3, 4);

            //define the connections to the .dbf file
            OleDbConnection conn = new OleDbConnection(@"Provider=vfpoledb;Data Source=" + DataSource + ";Collating Sequence=machine;");

            OleDbCommand command = new OleDbCommand("select ma_icd,rtrim(tenicd) as tenicd from " + DBF_FileName + " order by ma_icd", conn);
            //open the connection and read in all the data from .dbf file into a datatable
            conn.Open();
            DataTable dt = new DataTable();
            dt.Load(command.ExecuteReader());
            cboICD.ClearItems();
            cboICD.AllowSort = true;
            cboICD.DataSource = dt;
            cboICD.Splits[0].DisplayColumns[0].Width = 50;
            cboICD.Splits[0].DisplayColumns[1].Width = 400;
            conn.Close();  //close connection to the .dbf file

        }
        private void cboICD_SelectedValueChanged(object sender, EventArgs e)
        {
            txtTenbenh.Text = cboICD.Columns[1].Value.ToString().Trim(); 
        }
       
    }
}
